﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using DotMail.Commons.DAO;
using DotMail.Commons.Interfaces;

namespace MailServer.MailLib
{
    class Kernel
    {
        #region Fields
        List<PostOfficeInterface> postOfficePlugins;
        PostOfficeInterface mta;
        PostOfficeInterface mda;
        #endregion

        #region Constructors
        public Kernel()
        {
        }

        public Kernel(XmlNode config)
        {
            postOfficePlugins = new List<PostOfficeInterface>();
            // Parse the config and load up all desired modules
            ParseConfig(config);
        }
        #endregion

        #region Methods
        private void LoadPlugins(XmlNode plugins)
        {
            foreach (XmlNode plugin in plugins.SelectNodes("plugin"))
            {
                Assembly assembly = Assembly.LoadFrom(plugin.SelectSingleNode("assembly").InnerText);
                Object newObject = assembly.GetType(plugin.SelectSingleNode("pluginName").InnerText);
                if (newObject is PostOfficeInterface)
                {
                    PostOfficeInterface plugIn = newObject as PostOfficeInterface;
                    plugIn.LoadConfig(plugin.SelectSingleNode("config"));
                    plugIn.ChangeFolder += new changeFolder(plugIn_ChangeFolder);
                    plugIn.ChangeMessageAttributes += new changeMessageAttributes(plugIn_ChangeMessageAttributes);
                    plugIn.CreateFolder += new createFolder(plugIn_CreateFolder);
                    plugIn.DeleteMessage += new deleteMessage(plugIn_DeleteMessage);
                    plugIn.GetFolderList += new getFolderList(plugIn_GetFolderList);
                    plugIn.GetMessage += new getMessage(plugIn_GetMessage);
                    plugIn.GetMessageAttributes += new getMessageAttributes(plugIn_GetMessageAttributes);
                    plugIn.GetMessageList += new getMessageList(plugIn_GetMessageList);
                    plugIn.Login += new login(plugIn_Login);
                    plugIn.LogOut += new logout(plugIn_LogOut);
                    plugIn.MoveMessage += new copyMessage(plugIn_MoveMessage);
                    postOfficePlugins.Add(plugIn);
                }
            }
        }

        private void LoadStorage(XmlNode xmlNode)
        {
            throw new NotImplementedException();
        }

        private void ParseConfig(XmlNode config)
        {
            // Load the storage
            LoadStorage(config.SelectSingleNode("configuration/storage"));
            // Load plug-ins
            LoadPlugins(config.SelectSingleNode("configuration/plugins"));
            // Load MDA
            if (mda != null)
            {
                mda.StartListening();
            }
        }

        #region Event Handlers
        bool plugIn_ChangeFolder(Folder folder)
        {
            throw new NotImplementedException();
        }

        bool plugIn_ChangeMessageAttributes(int messageID, MessageAttributes attribs)
        {
            throw new NotImplementedException();
        }

        bool plugIn_CreateFolder(int parentFolderID)
        {
            throw new NotImplementedException();
        }

        bool plugIn_DeleteMessage(int messageID)
        {
            throw new NotImplementedException();
        }

        List<Folder> plugIn_GetFolderList(int userID)
        {
            throw new NotImplementedException();
        }

        Message plugIn_GetMessage(int messageID)
        {
            throw new NotImplementedException();
        }

        MessageAttributes plugIn_GetMessageAttributes(int messageID)
        {
            throw new NotImplementedException();
        }

        List<ListMessage> plugIn_GetMessageList(int userID, int folderID)
        {
            throw new NotImplementedException();
        }

        User plugIn_Login(string userName, string password, bool lockBit)
        {
            throw new NotImplementedException();
        }

        bool plugIn_LogOut(int userID)
        {
            throw new NotImplementedException();
        }

        bool plugIn_MoveMessage(int messageID, int sourceFolderID, int destFolderID)
        {
            throw new NotImplementedException();
        }

        #endregion
        #endregion
    }
}
